package com.huang.lesson02;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class TestServer {
    public static void main(String[] args) {
        InputStream is =null;
        ByteArrayOutputStream stream =null;
        Socket socket =null;
        //我要有个地址
        try {
            ServerSocket serverSocket = new ServerSocket(9999);
            //等待连接  返回客户端的socker
            socket = serverSocket.accept();
            //读取消息
            is = socket.getInputStream();
//            byte[] bytes = new byte[1024];
//            int len=0;
//            while ((len=is.read(bytes))!=-1){
//                System.out.println(new String(bytes, 0, len));
//            }

            //管道流
           stream = new ByteArrayOutputStream();
            byte[] bytes = new byte[1024];
            int len;
            while ((len=is.read(bytes))!=-1){
                stream.write(bytes,0,len);
            }

            System.out.println(stream.toString());

        } catch (IOException e) {
            e.printStackTrace();

        }finally {
            try {
                stream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
